import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 言初
 * Date: 2024-04-01
 * Time: 21:47
 */

class Users {
    public String username;
    public String password;
}


@WebServlet("/json")
public class JsonServlet extends HelloServlet{
    private ObjectMapper objectMapper = new ObjectMapper();
    // 使用jackson ,最核心的对象就是ObjectMapper
    // 通过这个对象，就可以把json字符串解析成Java对象，也可以把一个Java对象转成一个json格式字符串。
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Users users = objectMapper.readValue(req.getInputStream(),Users.class);
        System.out.println("username="+users.username+" password="+users.password);
        resp.getWriter().write("ok");
    }
}
